package com.aier.proxy.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.IcCard;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.comm.ProxyEquipPageSize;
import com.aier.proxy.entity.Proxy;
import com.aier.proxy.entity.ProxyCity;
import com.aier.proxy.service.IProxyService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("proxyCreateSubProxyAction")
public class ProxyCreateSubProxyAction extends ProxyBaseAction {

    /**
     * 创建子级代理商
     */
    private static final long serialVersionUID = 1L;
    @Resource(name = "proxyService")
    private IProxyService proxyService;
    @Resource(name = "userService")
    private IUserService userService;    
    
    
    // 页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    // 错误码
    private int s;
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;

    // 代理商ID
    private int pid;
    //代理商城市列表
    private List<ProxyCity> proCityList;
    private List<IcCard> head_searchList;
    //代理商级别
    private int ptype;
    //子级代理商
    private List<Proxy> subProxys;
    //添加子级代理商的城市id
    private int cid_add;
    //添加子级代理商的名字
    private String name_add;
    //要删除的代理商id
    private int pid_delete;
    private String account_add;
    private String error_info;
    //分派
    private String ic_head_add;
    private String body_begin;
    private String body_end;
    
    
    @Override
    public String execute() {
        head_searchList = proxyService.searchProCards(pid);
        proCityList = proxyService.searchProxyCity(pid);
        ptype = proxyService.searchProxyByPid(pid).getPtype();
        return SUCCESS;
    }

    /**
     * 加载
     * @return
     */
    @SuppressWarnings("unchecked")
    public String load(){
        if (page <= 0) {
            page = 1;
        }

        Map<String, Object> map = proxyService.searchSubProxys(
                page, ProxyEquipPageSize.PROXY_SUB, pid);
        if (null == map) {
            s = 0;
            return ERROR;
        }
        subProxys = (List<Proxy>) map.get(ICommonDAO.OBJ_LIST);
        if (null == subProxys) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 添加子级代理
     * @return
     */
    public String addSubProxy(){
        int sub_ptype = ProxyEquipPageSize.PROXY_PTYPE_FOUR;
        User user = userService.searchUserByCondition(account_add);
        if (null == user) {
            User u = new User(account_add, MD5Util.getMD5(account_add), name_add, "");
            if (0 >= userService.insertUserNormal(u)) {
                s = 0;
                error_info = "用户创建失败";
                return SUCCESS;
            }
            user = userService.searchUserByCondition(account_add);
        }else{
            List<Proxy> proxyList = proxyService.searchProxy(user.getUid());
            if(proxyList!=null&&proxyList.size()!=0){
                s = 0;
                error_info = "该账号已经是代理商";
                return SUCCESS;
            }
        }
        if(ptype<ProxyEquipPageSize.PROXY_PTYPE_FOUR){
            sub_ptype = ptype + 1;
        }
        s = proxyService.insertSubProxy(pid,sub_ptype,name_add,cid_add,user.getUid());
        return SUCCESS;
    }
    
    /**
     * 添加子级代理(经销商)
     * @return
     */
    public String addSubJxsProxy(){
        User user = userService.searchUserByCondition(account_add);
        if(user==null){
            s = 0;
            error_info = "该账号不存在";
            return SUCCESS;
        }
        List<Proxy> proxyList = proxyService.searchProxy(user.getUid());
        if(proxyList!=null&&proxyList.size()!=0){
            s = 0;
            error_info = "该账号已经是代理商";
            return SUCCESS;
        }
        s = proxyService.insertSubProxy(pid,ProxyEquipPageSize.PROXY_PTYPE_FOUR,name_add,cid_add,user.getUid());
        return SUCCESS;
    }
    
    /**
     * 删除子级代理
     * @return
     */
    public String deleteSubProxy(){
        s = proxyService.deleteProxy(pid_delete);
        return SUCCESS;
    }
    
    /**
     * 分派卡给子级代理
     * @return
     */
    public String assignSubCard(){
        List<IcCard> icList = proxyService.searchProCardUse(pid, body_begin, body_end,ic_head_add);//已分派
        if(icList!=null&&icList.size()>0){
            error_info = "卡号:" + ic_head_add + icList.get(0).getIcbody() + "已经分派";
            s = 0;
            return SUCCESS;            
        }else{
            s = proxyService.insertSubCard(pid,pid_delete,ic_head_add,body_begin, body_end);
        }
        return SUCCESS;
    }
    
    /**
     * 回收子级代理商的卡
     * @return
     */
    public String rebackSubCard(){
        List<IcCard> icList = proxyService.searchProCardUse(pid_delete, body_begin, body_end,ic_head_add);//已分派
        if(icList!=null&&icList.size()>0){
            error_info = "卡号:" + ic_head_add + icList.get(0).getIcbody() + "已经分派";
            s = 0;
            return SUCCESS;            
        }else{
            s = proxyService.updateBackSubCard(pid,pid_delete,ic_head_add,body_begin, body_end);
        }
        return SUCCESS;        
    }
    
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public int getS() {
        return s;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public void setProxyService(IProxyService proxyService) {
        this.proxyService = proxyService;
    }

    public List<ProxyCity> getProCityList() {
        return proCityList;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public List<Proxy> getSubProxys() {
        return subProxys;
    }

    public void setCid_add(int cid_add) {
        this.cid_add = cid_add;
    }

    public void setName_add(String name_add) {
        this.name_add = name_add;
    }

    public void setPid_delete(int pid_delete) {
        this.pid_delete = pid_delete;
    }

    public String getError_info() {
        return error_info;
    }

    public int getPtype() {
        return ptype;
    }

    public void setPtype(int ptype) {
        this.ptype = ptype;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setAccount_add(String account_add) {
        this.account_add = account_add;
    }

    public List<IcCard> getHead_searchList() {
        return head_searchList;
    }

    public void setIc_head_add(String ic_head_add) {
        this.ic_head_add = ic_head_add;
    }

    public void setBody_begin(String body_begin) {
        this.body_begin = body_begin;
    }

    public void setBody_end(String body_end) {
        this.body_end = body_end;
    }

    
}
